package com.loan.psychicloan.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.loan.psychicloan.R;
import com.loan.psychicloan.Util.ItemClickSupport;
import com.loan.psychicloan.Util.OkHttpUtils;
import com.loan.psychicloan.Util.RecycleViewDivider;
import com.loan.psychicloan.activity.BtnActivity;
import com.loan.psychicloan.activity.LoanInfoActivity;
import com.loan.psychicloan.activity.LoginActivity;
import com.loan.psychicloan.activity.MainActivity;
import com.loan.psychicloan.adapter.LoanAdapter;
import com.loan.psychicloan.bean.HomeBean;
import com.loan.psychicloan.bean.LoanListBean;
import com.loan.psychicloan.imageloder.BannerImageLoader;
import com.loan.psychicloan.imageloder.ImageLoader;
import com.loan.psychicloan.imageloder.ImageLoaderUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by watilion on 2017/12/18 0018.
 */

public class FragmentHome extends Fragment implements View.OnClickListener{

    Toolbar mtoolBar;

    private RecyclerView mRecyclerView;
    private List<HomeBean.BannerBean> mBanner = new ArrayList<>();
    private List<LoanListBean.DataBean> mRecommendList = new ArrayList <>();
    private List<LoanListBean.DataBean> mHotLoanList = new ArrayList <>();
    private List<HomeBean.BtnText> mBtenText = new ArrayList <>();
    private HomeBean.WeChat weChat = new HomeBean.WeChat();
    private Banner banner;
    ImageLoaderUtil imageLoaderUtil = new ImageLoaderUtil();
    ImageLoader.Builder builder = new ImageLoader.Builder();
    ImageLoader imageLoader;
    private TextView tv_btn_left;
    private TextView tv_btn_middle;
    private TextView tv_btn_right;
    private ImageView iv_hotloan_1;
    private ImageView iv_hotloan_2;
    private ImageView iv_hotloan_3;
    private ImageView iv_hotloan_4;
    private TextView tv_hotloan_1;
    private TextView tv_hotloan_2;
    private TextView tv_hotloan_3;
    private TextView tv_hotloan_4;
    private RelativeLayout rl_btn_left;
    private RelativeLayout rl_btn_middle;
    private RelativeLayout rl_btn_right;
    private RelativeLayout rl_hotloan_1;
    private RelativeLayout rl_hotloan_2;
    private RelativeLayout rl_hotloan_3;
    private RelativeLayout rl_hotloan_4;

    public FragmentHome() {
    }


    public static FragmentHome newInstance(){
        FragmentHome fragmentHome = new FragmentHome();
        Bundle bundle=new Bundle();
        bundle.putString("text", "首页");
        fragmentHome.setArguments(bundle);
        return fragmentHome;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)  {
        View view=inflater.inflate(R.layout.fragment_home,container,false);
        //textView= (TextView) view.findViewById(R.id.textView);
        //textView.setText(getArguments().getString("text"));
        mtoolBar = view.findViewById(R.id.main_toolbar);
        mtoolBar.inflateMenu(R.menu.main_toolbar_menu);

        mtoolBar.setOnMenuItemClickListener(itemListener);

        mRecyclerView = view.findViewById(R.id.recyclerview);
        banner = view.findViewById(R.id.banner);
        tv_btn_left = view.findViewById(R.id.tv_btn_left);
        tv_btn_middle = view.findViewById(R.id.tv_btn_middle);
        tv_btn_right = view.findViewById(R.id.tv_btn_right);
        iv_hotloan_1 = view.findViewById(R.id.iv_hotloan_1);
        iv_hotloan_2 = view.findViewById(R.id.iv_hotloan_2);
        iv_hotloan_3 = view.findViewById(R.id.iv_hotloan_3);
        iv_hotloan_4 = view.findViewById(R.id.iv_hotloan_4);
        tv_hotloan_1 = view.findViewById(R.id.tv_hotloan_1);
        tv_hotloan_2 = view.findViewById(R.id.tv_hotloan_2);
        tv_hotloan_3 = view.findViewById(R.id.tv_hotloan_3);
        tv_hotloan_4 = view.findViewById(R.id.tv_hotloan_4);

        rl_btn_left = view.findViewById(R.id.rl_btn_left);
        rl_btn_middle = view.findViewById(R.id.rl_btn_middle);
        rl_btn_right = view.findViewById(R.id.rl_btn_right);

        rl_hotloan_1 = view.findViewById(R.id.rl_hotloan_1);
        rl_hotloan_2 = view.findViewById(R.id.rl_hotloan_2);
        rl_hotloan_3 = view.findViewById(R.id.rl_hotloan_3);
        rl_hotloan_4 = view.findViewById(R.id.rl_hotloan_4);


        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new BannerImageLoader());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.HORIZONTAL));

        //开启网络下载数据的方法
        startTask();

        initOnClick();


        //设置item的点击事件监听
        ItemClickSupport mItemClickSupport = ItemClickSupport.addTo(mRecyclerView);
        mItemClickSupport.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, View itemView, int position) {
                Intent mIntent = new Intent(getActivity(), LoanInfoActivity.class);
                mIntent.putExtra("loanid",mRecommendList.get(position).loanid);
                Log.d("loanid", "onItemClicked: "+mRecommendList.get(position).loanid);

                startActivity(mIntent);
            }
        });
        return view;
    }
    private Toolbar.OnMenuItemClickListener itemListener = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            //Toast.makeText(getContext(),"登录", Toast.LENGTH_SHORT).show();
            if (MainActivity.LOGIN){
                Toast.makeText(getContext(),"您已登录，请勿重复登录",Toast.LENGTH_SHORT).show();
                return true;
            }else {
                Intent mIntent = new Intent(getActivity(), LoginActivity.class);
                startActivity(mIntent);
                return false;
            }
        }
    };

    private void initOnClick() {
        rl_btn_left.setOnClickListener(this);
        rl_btn_middle.setOnClickListener(this);
        rl_btn_right.setOnClickListener(this);
        rl_hotloan_1.setOnClickListener(this);
        rl_hotloan_2.setOnClickListener(this);
        rl_hotloan_3.setOnClickListener(this);
        rl_hotloan_4.setOnClickListener(this);
    }

    private void startTask() {
        //通过类名直接调用静态方法获取单例对象再调用getBeanOfOK()方法传入参数通过接口回调获取数据
        String URL = "https://www.psychicloan.com:8443/loan-api/home/gethome";
        OkHttpUtils.getInstance().getBeanOfOk(getActivity(), URL, HomeBean.class,
                new OkHttpUtils.CallBack <HomeBean>() {
                    @Override
                    public void getData(HomeBean homeBean) {
                        if (homeBean != null){
                            //如果不为空就用mLoanList接收
                            mRecommendList.addAll(homeBean.recommend);
                            mBtenText.addAll(homeBean.btntext);
                            mBanner.addAll(homeBean.banner);
                            mHotLoanList.addAll(homeBean.hotloan);
                            weChat.setOfficial_accounts(homeBean.wechat.official_accounts);
                            weChat.setWechat(homeBean.wechat.wechat);

                            saveWeChat();
                            initBanner();
                            initBtn();
                            initHotLoan();
                            initAdapter();
                        }
                    }
                });
    }

    private void saveWeChat() {
        MainActivity.WeChact = weChat;
        //Log.d("getOfficial_accounts", "saveWeChat: "+weChat.getOfficial_accounts());
        //Log.d("wechat", "saveWeChat: "+weChat.getWechat());
    }

    private void initHotLoan() {
        tv_hotloan_1.setText(mHotLoanList.get(0).loanname);
        imageLoader =builder.url(mHotLoanList.get(0).loanlogo).placeHolder(R.mipmap.ic_launcher).imageView(iv_hotloan_1).build();
        imageLoaderUtil.loadImage(getContext(),imageLoader);
        tv_hotloan_2.setText(mHotLoanList.get(1).loanname);
        imageLoader =builder.url(mHotLoanList.get(1).loanlogo).placeHolder(R.mipmap.ic_launcher).imageView(iv_hotloan_2).build();
        imageLoaderUtil.loadImage(getContext(),imageLoader);
        tv_hotloan_3.setText(mHotLoanList.get(2).loanname);
        imageLoader =builder.url(mHotLoanList.get(2).loanlogo).placeHolder(R.mipmap.ic_launcher).imageView(iv_hotloan_3).build();
        imageLoaderUtil.loadImage(getContext(),imageLoader);
        tv_hotloan_4.setText(mHotLoanList.get(3).loanname);
        imageLoader =builder.url(mHotLoanList.get(3).loanlogo).placeHolder(R.mipmap.ic_launcher).imageView(iv_hotloan_4).build();
        imageLoaderUtil.loadImage(getContext(),imageLoader);



    }

    private void initBtn() {
        //Logger.d("btn_text", "initBtn: "+mBtenText.toString());
        tv_btn_left.setText(mBtenText.get(0).btntext);
        tv_btn_middle.setText(mBtenText.get(1).btntext);
        tv_btn_right.setText(mBtenText.get(2).btntext);
    }

    private void initAdapter() {

        //创建自定义适配器对象
        LoanAdapter loanAdapter = new LoanAdapter(getContext(), mRecommendList);

        //设置recyclerview适配器
        mRecyclerView.setAdapter(loanAdapter);

        //刷新适配器
        loanAdapter.notifyDataSetChanged();
    }

    private void initBanner(){
        List<String> mbannerImg = new ArrayList <>();
        for (int i=0;i< mBanner.size();i++){
            mbannerImg.add(mBanner.get(i).loanimg);
        }
        banner.setImages(mbannerImg);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(2500);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent mIntent = new Intent(getActivity(),LoanInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("loanid", String.valueOf(mBanner.get(position).loanid));
                mIntent.putExtras(bundle);
                startActivity(mIntent);
                //Toast.makeText(getContext(),"第"+position+"个被点击了，id为"+mBanner.get(position).loanid, Toast.LENGTH_SHORT).show();
            }
        });
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    @Override
    public void onClick(View view) {

        Intent mIntent = null;
        switch (view.getId()){
            case R.id.rl_btn_left:
                 mIntent = new Intent(getContext(), BtnActivity.class);
                //Toast.makeText(getContext(), "左侧按钮", Toast.LENGTH_SHORT).show();
                mIntent.putExtra("btnid","1");
                mIntent.putExtra("btnname",mBtenText.get(0).btntext);
                mIntent.putExtra("btn_notice", getString(R.string.btn_left_notice));
                break;
            case R.id.rl_btn_middle:
                mIntent = new Intent(getContext(), BtnActivity.class);
                mIntent.putExtra("btnid","2");
                mIntent.putExtra("btnname",mBtenText.get(1).btntext);
                mIntent.putExtra("btn_notice", getString(R.string.btn_middle_notice));
                break;
            case R.id.rl_btn_right:
                mIntent = new Intent(getContext(), BtnActivity.class);
                mIntent.putExtra("btnid","3");
                mIntent.putExtra("btnname",mBtenText.get(2).btntext);
                mIntent.putExtra("btn_notice", getString(R.string.btn_right_notice));
                break;
            case R.id.rl_hotloan_1:
                mIntent = new Intent(getContext(),LoanInfoActivity.class);
                mIntent.putExtra("loanid", mHotLoanList.get(0).loanid);
                break;
            case R.id.rl_hotloan_2:
                mIntent = new Intent(getContext(),LoanInfoActivity.class);
                mIntent.putExtra("loanid", mHotLoanList.get(1).loanid);
                break;
            case R.id.rl_hotloan_3:
                mIntent = new Intent(getContext(),LoanInfoActivity.class);
                mIntent.putExtra("loanid", mHotLoanList.get(2).loanid);
                break;
            case R.id.rl_hotloan_4:
                mIntent = new Intent(getContext(),LoanInfoActivity.class);
                mIntent.putExtra("loanid", mHotLoanList.get(3).loanid);
                break;
            default:
                break;
        }
        startActivity(mIntent);
    }
}

